/**
 * Project day03
 */

#include "TaskQueue.h"
#include <iostream>
using std::cout;
using std::endl;


TaskQueue::TaskQueue(size_t queCapacity)
    : _capacity(queCapacity),
      _flag(true)
{
}


void TaskQueue::push(ElemType val)
{
    unique_lock<mutex> autoLock(_mutex);
    while (isFull())
    {
        _notFull.wait(autoLock);
    }
    _que.push(val);
    _notEmpty.notify_one();
    return;
}

TaskQueue::~TaskQueue()
{
}

/**
 * @return int
 */
ElemType TaskQueue::pop()
{
    unique_lock<mutex> autoLock(_mutex);
    while (isEmpty() && _flag)
    {
        _notEmpty.wait(autoLock);
    }
    if (_flag)
    {
        ElemType ret = _que.front();
        _que.pop();
        _notFull.notify_one();
        return ret;
    }
    else
    {
        return nullptr;
    }
}

/**
 * @return bool
 */
bool TaskQueue::isFull()
{
    return _que.size() == _capacity;
}

/**
 * @return bool
 */
bool TaskQueue::isEmpty()
{
    return _que.size() == 0;
}

void TaskQueue::wakeup()
{
    _flag = false;
    _notEmpty.notify_all();
}